﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace CommonMethod
{
    public class MyLogger
    {
        public static Object locknum = 0;

        public static string LOG_BASE_PATH = Application.StartupPath + "\\Log\\";
        /// <summary>
        /// 写日志文件
        /// </summary>
        /// <param name="str"></param>
        public static void Log(string str)
        {
            lock (locknum)
            {
                if (!Directory.Exists(LOG_BASE_PATH))
                {
                    Directory.CreateDirectory(LOG_BASE_PATH);
                }
                string timeStr = System.DateTime.Now.Year.ToString() + "." + System.DateTime.Now.Month.ToString() + "." + System.DateTime.Now.Day.ToString();
                string strLogPath = LOG_BASE_PATH + timeStr + ".txt";

                string fullTimeStr = System.DateTime.Now.Year.ToString() + "." + System.DateTime.Now.Month.ToString() + "." + System.DateTime.Now.Day.ToString() + "." + System.DateTime.Now.Hour.ToString() + "." + System.DateTime.Now.Minute.ToString() + "." + System.DateTime.Now.Second.ToString() + "." + System.DateTime.Now.Millisecond.ToString();
                FileStream fs = new FileStream(strLogPath, FileMode.Append);
                StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                sw.WriteLine(fullTimeStr + ":  " + str);
                sw.Close();
                fs.Close();
            }
        }

        public static void LogData(string str)
        {
            lock (locknum)
            {
                if (!Directory.Exists(LOG_BASE_PATH))
                {
                    Directory.CreateDirectory(LOG_BASE_PATH);
                }
                string timeStr = System.DateTime.Now.Year.ToString() + "." + System.DateTime.Now.Month.ToString() + "." + System.DateTime.Now.Day.ToString();
                string strLogPath = LOG_BASE_PATH + timeStr + ".txt";

                string fullTimeStr = System.DateTime.Now.Year.ToString() + "." + System.DateTime.Now.Month.ToString() + "." + System.DateTime.Now.Day.ToString() + "." + System.DateTime.Now.Hour.ToString() + "." + System.DateTime.Now.Minute.ToString() + "." + System.DateTime.Now.Second.ToString() + "." + System.DateTime.Now.Millisecond.ToString();
                FileStream fs = new FileStream(strLogPath, FileMode.Append);
                StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                sw.WriteLine(str);
                sw.Close();
                fs.Close();
            }
        }

        public static void LogData(string str, string keyName)
        {
            lock (locknum)
            {
                if (!Directory.Exists(LOG_BASE_PATH))
                {
                    Directory.CreateDirectory(LOG_BASE_PATH);
                }
                string timeStr = System.DateTime.Now.Year.ToString() + "." + System.DateTime.Now.Month.ToString() + "." + System.DateTime.Now.Day.ToString();
                string strLogPath = LOG_BASE_PATH + keyName + "_" + timeStr + ".txt";

                string fullTimeStr = System.DateTime.Now.Year.ToString() + "." + System.DateTime.Now.Month.ToString() + "." + System.DateTime.Now.Day.ToString() + "." + System.DateTime.Now.Hour.ToString() + "." + System.DateTime.Now.Minute.ToString() + "." + System.DateTime.Now.Second.ToString() + "." + System.DateTime.Now.Millisecond.ToString();
                FileStream fs = new FileStream(strLogPath, FileMode.Append);
                StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                sw.WriteLine(str);
                sw.Close();
                fs.Close();
            }
        }
        /// <summary>
        /// 写日志文件
        /// </summary>
        /// <param name="str"></param>
        /// <param name="strLogPath"></param>
        /// <param name="logTime"></param>
        public static void Log(string str, string strLogPath, bool logTime = false)
        {
            lock (locknum)
            {
                if (!Directory.Exists(Path.GetDirectoryName(strLogPath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(strLogPath));
                }
                string fullTimeStr = System.DateTime.Now.Year.ToString() + "." + System.DateTime.Now.Month.ToString() + "." + System.DateTime.Now.Day.ToString() + "." + System.DateTime.Now.Hour.ToString() + "." + System.DateTime.Now.Minute.ToString() + "." + System.DateTime.Now.Second.ToString() + "." + System.DateTime.Now.Millisecond.ToString();
                FileStream fs = new FileStream(strLogPath, FileMode.Append);
                StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                if (logTime)
                {
                    sw.WriteLine(fullTimeStr + "," + str);
                }
                else
                {
                    sw.WriteLine(str);
                }

                sw.Close();
                fs.Close();
            }
        }

        /// <summary>
        /// 写CSV文件
        /// </summary>
        /// <param name="Data">数据</param>
        /// <param name="strLogPath">保存路径</param>
        /// <param name="logTime">是否添加时间</param>
        public static void WriteCSV(List<string> Data, string strLogPath, bool logTime = false)
        {
            lock (locknum)
            {
                if (!Directory.Exists(Path.GetDirectoryName(strLogPath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(strLogPath));
                }
                string fullTimeStr = System.DateTime.Now.Year.ToString() + "." + System.DateTime.Now.Month.ToString() + "." + System.DateTime.Now.Day.ToString() + "." + System.DateTime.Now.Hour.ToString() + "." + System.DateTime.Now.Minute.ToString() + "." + System.DateTime.Now.Second.ToString() + "." + System.DateTime.Now.Millisecond.ToString();
                FileStream fs = new FileStream(strLogPath, FileMode.Append);
                StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                if (logTime)
                {
                    foreach (string tmpStr in Data)
                    {
                        sw.WriteLine(fullTimeStr + "," + tmpStr);
                    }
                }
                else
                {
                    foreach (string tmpStr in Data)
                    {
                        sw.WriteLine(tmpStr);
                    }
                }

                sw.Close();
                fs.Close();
            }
        }
        /// <summary>
        /// 读取CSV表格数据
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public List<List<string>> ReadCSV(string filePath)
        {
            List<List<string>> value = new List<List<string>>();

            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
            //记录每次读取的一行记录
            string strLine = "";
            //记录每行记录中的各字段内容
            string[] aryLine;


            //------------------------------添加表头数据------------------------//

            //读取表头数据，初始化N个数据列
            string[] Firstline;
            string FirstLineStr = "";
            if ((FirstLineStr = sr.ReadLine()) != null)
            {
                Firstline = FirstLineStr.Split(',');
                for (int i = 0; i < Firstline.Length; i++)
                {
                    value.Add(new List<string>());
                    value[i].Add(Firstline[i]);//添加表头数据
                }
            }

            //------------------------------添加表头数据------------------------//

            //----------------------------------逐行读取数据------------------------//
            while ((strLine = sr.ReadLine()) != null)
            {

                aryLine = strLine.Split(',');
                for (int i = 0; i < aryLine.Length; i++)
                {
                    value[i].Add(aryLine[i]);
                }
                //-----------------------------------按行读取数据------------------------------//

                //List<string> CurrentLine = new List<string>();               
                //for (int i = 0; i < aryLine.Length; i++)               
                //{             
                //    CurrentLine.Add(aryLine[i]);                
                //}            
                //value.Add(CurrentLine);

                //-----------------------------------按行读取数据------------------------------//

            }
            //----------------------------------逐行读取数据------------------------//

            sr.Close();
            fs.Close();
            return value;
        }
    }
}

